package com.entor.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.entor.dao.CocsDaoI;
import com.entor.dao.CocsInfoDaoI;
import com.entor.dao.DickDaoI;
import com.entor.model.CocAndInfo;
import com.entor.model.Cocs;
import com.entor.model.CocsInfo;
import com.entor.model.Dick;
import com.entor.model.Init;
import com.entor.model.Msg;
@Service
public class CocsService implements BaseServiceI<Cocs>{
	
	@Autowired
	private CocsDaoI cdi;
	@Autowired
	private CocsInfoDaoI cidi;
	@Autowired
	private DickDaoI ddi;

	public List<Cocs> find(Cocs t) {
		List<Cocs> list = cdi.find(t);
		return list;
	}

	public Cocs get(Cocs t) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg insert(Cocs t) {
		Msg m = new Msg("保存失败", false);
		int i = cdi.insert(t);
		if(i>0) m = new Msg("保存成功", true ,t);
		return m;
	}

	public Msg batchInsert(String objs) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg edit(Cocs t) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg remove(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg batchRemove(String ids) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Msg saveInfo(CocAndInfo cai) {
		Cocs coc = new Cocs();
		//属性值拷贝
		BeanUtils.copyProperties(cai, coc);
		Msg m = this.insert(coc);
		if(m.isIs()){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			List<CocsInfo> list = new ArrayList<CocsInfo>();
			try{
				Calendar c = Calendar.getInstance();
				c.clear();
				for(int i=0;i<coc.getLevelNum();i++){
					String cdate = cai.getCocConndate().split(",")[i];
					String sdate = cai.getSecrConndate().split(",")[i];
					CocsInfo ci =new CocsInfo
						(coc.getCode()+"", "", 
						cai.getCocMemid()==null?null:cai.getCocMemid().split(",")[i],
						cai.getCocWorkdate()==null?null:sdf.parse(cai.getCocWorkdate().split(",")[i]),
						cai.getCocConndate()==null?null:cdate.equals("")?c.getTime():sdf.parse(cdate),
						"",
						cai.getSecrMemid()==null?null:cai.getSecrMemid().split(",")[i],
						cai.getSecrWorkdate()==null?null:sdf.parse(cai.getSecrWorkdate().split(",")[i]),
						cai.getSecrConndate()==null?null:sdate.equals("")?c.getTime():sdf.parse(sdate),
						cai.getIsNow()==null?null:cai.getIsNow().split(",")[i],
						cai.getLevleNum()==null?null:cai.getLevleNum().split(",")[i]);
					list.add(ci);
				}
				int i = cidi.insert(list);
				if(i>0){
					ApplicationContext ac = 
							new ClassPathXmlApplicationContext("init.xml");
					Init init = (Init)ac.getBean("initClass");
					Dick d = new Dick(coc.getCode(), init.getCocsSize(), 0+""); 
					ddi.insert(d);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return m;
	}

}
